use uuid::Uuid;
use crate::websockt::WebSocketFrame;
use super::WebSocketSession;

#[async_trait::async_trait]
pub trait WebSocketHandler: Send + Sync {
    async fn on_open(&mut self, id: &Uuid, session: &WebSocketSession);
    async fn on_message(&mut self, id: &Uuid, session: &WebSocketSession, msg: &str);
    async fn on_close(&mut self, id: &Uuid, session: &WebSocketSession);

    async fn on_close_with_back(&mut self, id: &Uuid, session: &WebSocketSession) {
        self.on_close(id, session).await;
        session.send_frame(WebSocketFrame::server_close()).await;
    }
}

pub struct DefaultHandler;

impl DefaultHandler {
    pub fn new() -> Self {
        Self
    }
}

#[async_trait::async_trait]
impl WebSocketHandler for DefaultHandler {
    async fn on_open(&mut self, id: &Uuid, _session: &WebSocketSession) {
        println!("On open: id: {id}");
    }

    async fn on_message(&mut self, id: &Uuid, _session: &WebSocketSession, msg: &str) {
        println!("On messgae: id: {id}, message: {msg}");
    }

    async fn on_close(&mut self, id: &Uuid, _session: &WebSocketSession) {
        println!("On clsoe: {id}")
    }
}